OTV Miner Class Structure


2. OTV Miner Class Structure



2.1. The OTV Miner game concept has been laid out. The next part of the process is to define the class objects that will make up the game play. My partner and I decided on the following class structure. This class is the base for all of our objects.


LP3-21.jpg



2.1.1. Here is the code implementation:


LP3-211.jpg


2.1.2. The GameObject has one constructor.


LP3-212.jpg


2.1.3. And the remaining components in the class are get/set methods to access the individual class components.


LP3-213.jpg
LP3-213A.jpg
LP3-213B.jpg


2.2. The Ship class inherits from the GameObject and is used as the main object for the game. The main ones completed are the movement actions.


LP3-22.jpg



2.2.1. The class starts off by implementing the characteristics that the ship class has that are unique to it.


LP3-221.jpg


2.2.2. The ship class has one constructor.


LP3-222.jpg


2.2.3. Here are the standard get/set methods.


LP3-223.jpg
LP3-223A.jpg
LP3-223B.jpg


2.2.4. The LoadContent method sets us the sound for the ship. At this time I don’t have a sound attached to it.


LP3-224.jpg


2.2.5. The RotateShip gets input from the gamepad of keyboard and translates it to movement.


LP3-225.jpg


2.2.6. The MoveShip method takes input from the keyboard/gamepad and translates it to movement. This method works with the new quadrant class and looks for the ship to leave the screen. When the ship reaches the top of the screen, for example, the method checks if the current quadrant can exited. If it can the quadrant is changed based upon the direction you are going and the ship is drawn to the appropriate part of the screen. It also looks to see if the ship is moving (by using the up or down arrows). If it is the moving variable is set to true so the ship animation can be drawn.


LP3-226.jpg
LP3-226A.jpg


2.2.7. The method continues with the key down logic.


LP3-227.jpg
LP3-227A.jpg
LP3-227B.jpg




2.3. The Quadrant class was created so I can track movement in the game and distribute rocks, elements, and enemies in the game. It is a straight forward implementation of a square grid with odd number dimensions. The smallest size (used in testing) is a 3x3 grid with a total of nine quadrants. By setting the constants in the code you can easily change the size of the quadrant space.


LP3-23.jpg



2.3.1. The class starts with the main variables that keep track of the actions. The curquad member holds the current quadrant. The MaxQuadrants (in the Constants Class) variable defines the quadrant size you want. In the game we are using a 5x5 playing area which gives us 25 areas where we can place objects to interact with. STARTQUAD (in the Constants Class) is the middle position where you start. I also set up a random variable to put objects in the game with. The constructor sets the starting quadrant.


LP3-231.jpg


2.3.2. The property setting is for the most part straight forward. QUADOFFSET (in the Constants Class) is how many quadrants you need to move when you go up or down. The MoveUp method subtracts the offset constant and updates the current quadrant. The CheckMoveUp method calls a private function that iterates through the quadrants to find the current row you are at. If the row is one than your current position you can’t move up.


LP3-232.jpg


2.3.3. The next set of methods are the remaining movement checks. You have to verify you can move before you actually move.


LP3-233.jpg


2.3.4. The two private functions in the class find the current row or column based upon the current quadrant. The main part of this function is to check the modulus of the loop counter and compare it to the offset constant. If it is zero then you are at the last column and thus the next number will be the next row. The loop goes until the loop counter is equal to the current quadrant.


LP3-234.jpg


2.3.5. The last method returns a random quadrant for object population.


LP3-235.jpg


2.4. The Animation class was modeled after the class used in the book “A Simple Introduction to Game Programming With C# and XNA 3.1 (C. Bennett)”. The class offered all of the functionality that I needed to make my animation work in this project.


LP3-24.jpg



2.4.1. The class starts out by creating a structure object to hold each image in the animation.


LP3-241.jpg


2.4.2. The class defines several tracking variables. One to check to see if looping is enabled, it the animation is stopped, and if the animation is playing. The most important part of the class is the List object that holds all of the frames for the animation. I added the position, rotation and center members to fit in with my objects.


LP3-242.jpg


2.4.3. The add cell method is how you add each image for the animation. I modified this from the original by centering the image location. When I was first using this class everything was centered on the bottom left corner. When you create an Animation object all you have to do is pass a position variable to establish a location. There are also two ways to set the position, you can pass in the X & Y coordinates or you can vend in a Vector2 object. I added the SetRotation method so I could use the ship animation in the game. The SetMoveRight and SetMoveLeft methods are not used in the game in this point. It flips the image back and forth for side to side movement.


LP3-243.jpg
LP3-243A.jpg


2.4.4. The main method in the class is the LoopAll function. It sets the looping member variable and gives a starting and ending cell. It sets the current cell and a time shift variable. The loop method has a set starting point and ending point.


LP3-244.jpg


2.4.5. The GotoFrame method sets the current image to the specified index.


LP3-245.jpg


2.4.6. The PlayAll method sets the environment to play all of the frames. The Play method starts at the given start position and ends at the specified end position.


LP3-246.jpg


2.4.7. The Draw method looks to see what cell is currently active and if the list is empty, before the first image, or over the total images it returns without drawing. The update method is what does all of the animation. It checks to see if the image is stopped. If so nothing happens. If the image is not stopped then it evaluates the total time against the time shift. If total time is greater than it resets the total time the variable, increments the current cell, and checks to see if looping is turned on. If it is and the cell count is at the end, it resets to the beginning. If the animation is set for one pass and it is at the end, the playing variable is turned off and the current cell is set to the last frame.


LP3-247.jpg


2.4.8. The last three methods are ones I added. I placed all of the image setup in here to make my code a little cleaner.


LP3-248.jpg
LP3-248A.jpg
LP3-248B.jpg



2.5. The AsteroidObject Class simply tracks the current state of the asteroid object.


LP3-25.jpg



2.5.1. The AsteroidObject class tracks the type of asteroid the object (i.e. small, medium, or large)


LP3-251.jpg


2.5.2. The AsteroidObject class also defines an enumeration of asteroid sizes.


LP3-252.jpg


2.6. The ElementObject class is as simple as the AsteroidObject class. It tracks its type (i.e. os3, laser fuel, propulsion fuel) and the objects collection value.


LP3-26.jpg



2.6.1. Definition of the class. Sets the current type when created and randomly finds a value for the element when collected.


LP3-261.jpg


2.6.2. The ElementObject class also defines an enumeration of element types.


LP3-262.jpg


2.7. The Menu class sets up the initial screen the user sees when they start the game. The can see the keyboard layout, the story behind the game, and set the difficulty level.


LP3-27.jpg



2.7.1. I start the class out by defining a structure with each image for the menu item. The I establish some tracking variables and constants.


LP3-271.jpg


2.7.2. Next I load all of the menu options the user will encounter. I also have the AddCell method to add each image to the objects list of menu items.


LP3-272.jpg


2.7.3. The next methods set up the ExitState of the program so it knows when to exit. The StartState is used to track if the user is playing the game. I also store the difficulty level the user selects. By default it is set to 2.


LP3-273.jpg


2.7.4. Next is the largest method of the class. The update method tracks the keys the user selects and shows the appropriate menu item. If the arrow keys are used it moves through the menu items.


LP3-274.jpg
LP3-274A.jpg


2.7.5. If the user hits enter the method looks to see what item was selected. If 1 is selected it starts the game. If selected is greater than zero (you are in a sub menu) it takes you back to the main menu. If you are in the main menu, it will set the selected variable to the selected menu. If the escape key is pressed it checks to see what state the game is in. If it is in the menu state it closes the program down. If you are in the middle of the game it takes you back to the main screen.


LP3-275.jpg


2.7.6. The draw method looks at where the menu selection is at. It displays the appropriate menu for the current selection.


LP3-276.jpg


2.8. We took all of the constants in each class and put them into the Constants class.


LP3-28.jpg



2.8.1. You can control all of the constant variables from this class.


LP3-281.jpg

Project Home Next Section